package com.codeoss.framework.view.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IVarName;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.chart.AbstractChart;
import com.codeoss.framework.view.component.chart.ChartDataItemDef;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.service.IChartComponentService;
import com.codeoss.framework.view.service.IVariableService;

@Service("gaugeComponentService")
public class GaugeComponentService extends ComponentService implements IChartComponentService{
	@Autowired
	private IScriptServiceStore scriptServiceStore;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IVariableService variableService;
	public void getChartData(YunContext yunContext, Component component) {
		AbstractChart abstractChart = (AbstractChart) component;
		Page page = yunContext.getPage();
		DataSourceConfig dataSourceConfig = page.getDataSourceConfigById(abstractChart.getDataSource());
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("SELECT ");
		List<ChartDataItemDef> measureList = abstractChart.getMeasureList();
		for (int i = 0; i < measureList.size(); i++) {
			ChartDataItemDef chartDataItemDef = measureList.get(i);
			if("average".equals(chartDataItemDef.getAggregateType())) {
				sqlBuilder.append("avg(");
			}else {
				sqlBuilder.append(chartDataItemDef.getAggregateType()).append("(");
			}
			sqlBuilder.append(chartDataItemDef.getField());
			sqlBuilder.append(")");
		}
		sqlBuilder.append(" FROM ");
		sqlBuilder.append(dataSourceConfig.getTableName());
		Expression whereClauseExpression=dataSourceConfig.getWhereClause();
		boolean hasWhere=false;
		if(whereClauseExpression!=null) {
			IScriptService scriptService=scriptServiceStore.getScriptService(whereClauseExpression);
			String whereClause=(String)scriptService.eval(whereClauseExpression.getScript(), yunContext.getVarValueMap());
			if(!StringUtils.isEmpty(whereClause)) {
				sqlBuilder.append(" WHERE ");
				sqlBuilder.append(whereClause);
				hasWhere=true;
			}
		}
		whereClauseExpression = abstractChart.getWhereClauseExpression();
		if (whereClauseExpression != null) {
			IScriptService scriptService = scriptServiceStore.getScriptService(whereClauseExpression);
			String whereClause = (String) scriptService.eval(whereClauseExpression.getScript(),
					yunContext.getVarValueMap());
			if (!StringUtils.isEmpty(whereClause)) {
				if(!hasWhere) {
					sqlBuilder.append(" WHERE ");
				}else {
					sqlBuilder.append(" AND ");
				}
				sqlBuilder.append(whereClause);
				hasWhere = true;
			}
		}
		Object para[]=new Object[]{};
		List<IDataBinding> searchDataBindingList=page.getAttribute("key_search_field_list");
		if(searchDataBindingList!=null) {
			List<Object> valueList=new ArrayList<Object>();
			for(int i=0;i<searchDataBindingList.size();i++) {
				IDataBinding dataBinding=searchDataBindingList.get(i);
				Object value=variableService.getVariableValue(yunContext, ((IVarName)dataBinding).getVarName());
				if(value!=null) {
					if(!hasWhere) {
						sqlBuilder.append(" WHERE 1=1 ");
						hasWhere=true;
					}
					valueList.add(value);
					String fieldInfos=page.getPageContext("field_"+dataBinding.getDataField());
					if(!StringUtils.isEmpty(fieldInfos)) {
						String fieldInfoArray[]=fieldInfos.split("[|]");
						sqlBuilder.append(" AND ").append(fieldInfoArray[0]).append(fieldInfoArray[1]).append("?");
					}else {
						sqlBuilder.append(" AND ").append(dataBinding.getDataField()).append("=?");
					}
				}
			}
			if(valueList.size()>0) {
				para=valueList.toArray();
			}
		}
		Long value=jdbcTemplate.queryForObject(sqlBuilder.toString(),Long.class,para);
		yunContext.setAttribute(component.getId()+"_chartdata", value);
	}
}
